<?php

namespace app\dispute\logic;

use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;

class DisputeApiLogic
{
    public function __construct()
    {
        
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $date = $params['date'] ?? "";
        $limit = $params['limit'] ?? 10;
        $where = [];

        if (!empty($date)) {
            $where[] = ['timedate', '=', strtotime($date)];
        }
        
        $list = Db::name('report_monthly_dispute')
            ->where($where)
            ->order('timedate desc')
            ->paginate($limit);

        $lists = $list->items();

        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $header = [];
        $header[] = ['月份', 'month'];
        $header[] = ['通道', 'type'];
        $header[] = ['总单量', 'total_order'];
        $header[] = ['赢单量', 'won'];
        $header[] = ['输单量', 'lost'];
        $header[] = ['审核中', 'under_review'];
        $header[] = ['待处理', 'needs_response'];
        $header[] = ['争议总销售额', 'dispute'];

        $date = $params['date'] ?? "";
        $where = [];

        if (!empty($date)) {
            $where[] = ['timedate', '=', strtotime($date)];
        }
        
        $lists = Db::name('report_monthly_dispute')
            ->where($where)
            ->order('timedate desc')
            ->select()->toarray();

        $fileName = "export_Advaccount" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

    public function add($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $result = false;

        try {
            if ($result !== false) {
                return json(DefaultResponse(200, true, '添加成功!', null));
            } else {
                return json(DefaultResponse(201, false, '添加失败!', null));
            }
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '广告账户已存在!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));

    }

    public function find($params = [])
    {
        $timedate = $params['timedate'] ?? "";
        $type = $params['type'] ?? "";
        $limit = $params['limit'] ?? 10;
        
        if (empty($timedate) || empty($type)) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $firstday = date("Y-m-d", $timedate);
        $end_time = strtotime(date("Y-m-d", strtotime("$firstday +1 month"))) - 1;

        $where = [];
        $where[]  = ['createtime', '>=', $timedate];
        $where[]  = ['createtime', '<=', $end_time]; 
        $where[]  = ['disputeapi_type', '=', $type]; 
        
        $list = Db::name('order_achievement')
            ->where($where)
            ->paginate($limit);
        
        $lists = $list->items();

        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    public function edit($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $id = $data['id'];
        if (empty($id)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        $result = false;

        try {
            if ($result) {
                return json(DefaultResponse(200, false, '更新成功!', null));
            }
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));
    }
    
}
